<?php

class DefaultController extends FController {

    public $user_school_list;
    public $user_subject_list;
    public $user_role_list;
    public $exam_id;
    //权限
    public function accessRules() {
        $config = array(
            'index' => 1,
            'taskadd' => 1
        );
        return ZRole::auth("space", $config);
    }

    public function __common() {
        parent::init();
        if (empty($this->cLoginUser)) {
            $this->redirect($this->createUrl('/site/login'));
        }else{
            $userLoginUser = $this->cLoginUser;
            $this->user_role_list = isset($userLoginUser['cuser']['role_id']) ? $userLoginUser['cuser']['role_id'] : '';
            $this->user_school_list = isset($userLoginUser['cuser']['school']['school_id']) ? $userLoginUser['cuser']['school']['school_id'] : '';
            $this->user_subject_list = isset($userLoginUser['cuser']['school']['subject_id']) ? $userLoginUser['cuser']['school']['subject_id'] : '';
            $zRow = new ZRole();
            //得到学校对应的考试
            $exam_list = $zRow->_getSchoolExam($this->user_school_list);
            if(!empty($exam_list)){
                $this->exam_id = implode(',', $exam_list);
            }
        }
        $this->commonScript();
        Yii::app()->clientScript->registerCssFile(THEME_CSS_URL . '/module_Sip.css');
    }

    //管理员任务查询页
    public function actionIndex() {
        $this->__common();
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/My97DatePicker/WdatePicker.js");
        $this->title = "管理员任务维护";
        $user_id = $this->cLoginUser;
        $page = (int) $this->request->getParam('page', 1);
        $s['exam_id'] = (int) $this->request->getParam('exam_id');
        $s['user_id'] = (int) $this->request->getParam('user_id');
        $s['role_id'] = (int) $this->request->getParam('role_id');
        $s['task_expired'] = (string) $this->request->getParam('task_expired');
        $s['task_status'] = (int) $this->request->getParam('task_status');
        $s = ZTask::dis_input($s);
        $s = Func::fAddslashes($s);

        $where = '';
        if(empty($s['exam_id'])){
         //权限判断
        if($this->user_role_list !=1 && !empty($this->exam_id)){
            $where .= ' "exam_id" IN ('.$this->exam_id.')';
           }
        }
        $zExam = new zExam();
        $exam = $zExam->_getExamObject($this->exam_id, $this->user_role_list);
        $user = ZTask::_getUserAll();
        $role = ZTask::_getAllrole();
        $taskinfo = ZTask::_getTaskAll($s);
        if ($taskinfo) {
            foreach ($taskinfo['listDatas'] as $key => $one) {
                $zuser = new ZUser();
                $task_user = $zuser->_getUserInfo($one['user_id']);
                $task_exam = ZTask::_getExamOne($one['exam_id']);
                if ($task_user) {
                    $taskinfo['listDatas'][$key]['username'] = $task_user['username'];
                } else {
                    $taskinfo['listDatas'][$key]['username'] = '';
                }
                if ($task_exam) {
                    $taskinfo['listDatas'][$key]['exam_term_name'] = $task_exam['exam_term_name'];
                } else {
                    $taskinfo['listDatas'][$key]['exam_term_name'] = '';
                }
            }
        }
        $pageUrl = $this->createUrl('/task/default/index', $s);
        $this->render('index', array(
            'exam' => $exam,
            'user' => $user,
            'role' => $role,
            'user_id' => $user_id['cuser']['id'],
            'role_id' => $user_id['cuser']['role_id'],
            'pageUrl' => $pageUrl,
            'page' => $page,
            'serch' => $s,
            'taskinfo' => $taskinfo['listDatas'],
            'pages' => $taskinfo['pages'],
        ));
    }

    //添加管理员任务页面
    public function actionTaskadd() {
        Yii::app()->clientScript->registerScriptFile(STATICS_URL . "/js/My97DatePicker/WdatePicker.js");
        $this->__common();
        $zExam = new zExam();
        $exam = $zExam->_getExamObject($this->exam_id, $this->user_role_list);
        $user = ZTask::_getUserAll();
        if ($_POST) {
            $task = new Task();
            $_POST = ZTask::dis_input($_POST);
            $task->exam_id = $_POST['exam_id'];
            $task->user_id = $_POST['user_id'];
            $task->task_name = $_POST['task_name'];
            $task->task_status = $_POST['task_status'];
            $task->task_expired = strtotime($_POST['task_expired']);
            $task->time_created = time();
            $task->update_user = $this->cLoginUser['cuser']['id'];
            $task->update_time = time();
            if ($task->save()) {
                $this->frameAjaxMessage('/task/default/index');
            } else {
                $this->frameAjaxMessage(null, '参数保存错误 请注意填写');
                exit;
            }
        }
        $this->renderPartial("taskadd", array('exam' => $exam, 'user' => $user), true);
    }

    //管理员任务保存 
    public function actionTaskedit() {
        $taskinfo = array();
        $zExam = new zExam();
        $exam = $zExam->_getExamObject($this->exam_id, $this->user_role_list);
        $user = ZTask::_getUserAll();
        $clogUser = $this->cLoginUser;
        $role_id = $clogUser['cuser']['role_id'];
        $task_id = (int) $this->request->getParam('task_id');
        if ($task_id) {
            $task = new Task();
            $taskinfo = $task->findByPk($task_id);
            if ($taskinfo) {
                $taskinfo = $taskinfo->attributes;
                $ztaskinfo = ZTask::_getExamOne($taskinfo['exam_id']);
                if ($ztaskinfo) {
                    $taskinfo['exam_term_name'] = $ztaskinfo['exam_term_name'];
                } else {
                    $taskinfo['exam_term_name'] = '';
                }
                $userinfo = new ZUser();
                $userone = $userinfo->_getUserInfo($taskinfo['user_id']);
                if ($userone) {
                    $taskinfo['username'] = $userone['username'];
                } else {
                    $taskinfo['username'] = '';
                }
            }
        }
        if ($_POST) {
            $task = new Task();
            $_POST = ZTask::dis_input($_POST);
            $task = $task->findByPk($_POST['task_id']);
            $task->user_id = $_POST['user_id'];
            $task->task_status = $_POST['task_status'];
            $task->exam_id = $_POST['exam_id'];
            $task->task_name = $_POST['task_name'];
            $task->task_expired = strtotime($_POST['task_expired']);
            if ($task->save()) {
                $this->frameAjaxMessage('/task/default/index');
            } else {
                $this->frameAjaxMessage(null, '参数保存错误 请注意填写');
                exit;
            }
        }
        $this->renderPartial("taskedit", array('exam' => $exam, 'user' => $user, 'taskinfo' => $taskinfo, 'role_id' => $role_id), true);
    }

    //删除管理员任务
    public function actionDeletetask() {
        $task_id = (int) $this->request->getParam('task_id');
        $task = new Task();
        if ($task->deleteByPk($task_id)) {
            echo 1;
        } else {
            echo 2;
        }
    }
    
	/**
	* @ 批量删除学段
	* @ yindebin
	* @ 2014-01-06 pm
	*
	*/
	public function actionDelete_all(){
		$id = Yii::app ()->request->getparam ( 'id' );
		if (! empty ( $id )) {
			$delete = 'DELETE FROM "{{task}}" WHERE "task_id" IN ('.$id.')';
			$query = Yii::app()->db->createCommand($delete)->execute();
			echo 1;
		}else{
			echo 2;
		}
	}
}